package com.cc.data_explore.gui.tab;

import atlantafx.base.theme.Styles;
import de.saxsys.mvvmfx.FxmlView;
import de.saxsys.mvvmfx.InjectViewModel;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;

/**
 * PrimaryDialogView
 *
 * @author aa
 * @version 1.0
 * @since 2025/4/1
 */
public class CreateTableDialogView implements FxmlView<CreateTableDialogViewModel> {

    @FXML
    public TextField headLineText;
    @FXML
    public TextField targetTableNameText;
    @FXML
    public TableView<CreateTableDialogData> primaryTableView;
    @FXML
    public TableColumn<CreateTableDialogData, Boolean> selected;
    @FXML
    public TableColumn<CreateTableDialogData, String> sourceField;
    @FXML
    public TableColumn<CreateTableDialogData, String> targetField;
    @FXML
    public TableColumn<CreateTableDialogData, FiledTypeEnum> filedType;
    @FXML
    public Button completeBtn;

    @InjectViewModel
    private CreateTableDialogViewModel viewModel;


    @FXML
    public void initialize() {
        selected.setCellValueFactory(new PropertyValueFactory<>("selected"));
        selected.setCellFactory(CheckBoxTableCell.forTableColumn(selected));
        sourceField.setCellValueFactory(new PropertyValueFactory<>("sourceField"));
        targetField.setCellValueFactory(new PropertyValueFactory<>("targetField"));
        targetField.setCellFactory(cell -> new EditingCell<>());
        // 设置下拉框选项
        ObservableList<FiledTypeEnum> departments = FXCollections.observableArrayList(FiledTypeEnum.values());
        // 使用 ComboBoxTableCell 渲染和编辑
        filedType.setCellFactory(ComboBoxTableCell.forTableColumn(departments));
        filedType.setCellValueFactory(cellData -> cellData.getValue().filedTypeProperty());
        headLineText.focusedProperty().addListener((observable, oldValue, newValue) -> {
            viewModel.setPrimaryDialogViewModel();
        });
        headLineText.setFocusTraversable(false);
        targetTableNameText.setFocusTraversable(false);
        headLineText.textProperty().bindBidirectional(viewModel.headLineTextProperty());
        targetTableNameText.textProperty().bindBidirectional(viewModel.targetTableNameTextProperty());
        primaryTableView.setItems(viewModel.getItems());
        completeBtn.getStyleClass().add(Styles.ACCENT);
    }

    public void completeBtnFun(ActionEvent actionEvent) {
        viewModel.completeBtnFun();
    }
}
